package com.dzl.manager.impl;

import com.dzl.admin.dao.SysAdminMapper;
import com.dzl.admin.pojo.SysAdmin;
import com.dzl.admin.pojo.dto.SendMsgDto;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.SmsConstant;
import com.dzl.enterprise.pojo.WelfareEnterprise;
import com.dzl.enterprise.service.WelfareEnterpriseService;
import com.dzl.manager.ManagerService;
import com.dzl.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private SysAdminMapper sysAdminMapper;
    @Autowired
    private WelfareEnterpriseService welfareEnterpriseService;

    @Override
    public String sendCode(Integer enterpriseId, String phone, String type) {
        if (EmptyUtils.isEmpty(type)) {
            type = "1000";
        }
        String code = "";
        String content = "";
        switch (type) {
            case "1"://注册发送
                //判断手机号是否已注册
                SysAdmin query = new SysAdmin();
                query.setPhone(phone);
                query.setEnterpriseId(enterpriseId);
                query.setStatus(CommonConstants.YES);
                List<SysAdmin> admins = sysAdminMapper.select(query);
                SysAdmin admin = EmptyUtils.isNotEmpty(admins)?admins.get(0):null;
                if (!EmptyUtils.isEmpty(admin)) {
                    throw new CommonException(HttpCode.MOBILE_REGISTERED.getMsg());
                }
                code = StringUtil.getRandomStringHasOnlyNumber(6);
                content = String.format(SmsConstant.SMS_REGISTER, code);
                break;
            case "1001":
                code = StringUtil.getRandomStringHasOnlyNumber(6);
                content = String.format(SmsConstant.SMS_BIND_CARD, code);
                break;
            case "1002":
                code = StringUtil.getRandomStringHasOnlyNumber(6);
                content = String.format(SmsConstant.SMS_UPDATE_PAYMENT_PASSWORD, code);
                break;
            case "1000":
                code = StringUtil.getRandomStringHasOnlyNumber(6);
                content = String.format(SmsConstant.SMS_YZM, code);
            default:
                break;
        }
        log.info("***********生成的验证码为：" + code);
        WelfareEnterprise welfareEnterprise = welfareEnterpriseService.selectByPrimaryKey(enterpriseId);
        if(!enterpriseId.equals(0) && EmptyUtils.isNotEmpty(welfareEnterprise)){
            content  = content.replace("【福力兔优选】","【"+welfareEnterprise.getSmsSignature()+"】");
            content  = content.replace("【企业福利】","【"+welfareEnterprise.getSmsSignature()+"】");
        }
        boolean isSuccess = SmsUtil.sendCLSms(content, phone);
        if (isSuccess) {
            RedisUtil.set(phone + ":" + type, code, 5 * 60);
        } else {
            throw new CommonException("短信发送失败，请联系管理员");
        }
        return code;
    }

    @Override
    public void sendMsg(SendMsgDto sendMsgDto) {
        String content = "";
        switch (sendMsgDto.getType()) {
            case "1":
                content = "【福力兔优选】您已成功购买：" + sendMsgDto.getGoodsName() + " ，兑换码为：" + sendMsgDto.getCode() + "，有效期至" + sendMsgDto.getTime() + "，您也可以在订单详情中查看兑换码，有任何疑问请联系客服";
//                content = String.format(SmsConstant.SMS_VIRTUAL_ORDER,sendMsgDto.getGoodsName(),sendMsgDto.getCode(),sendMsgDto.getTime());
                break;
            case "2":
                content = "【福力兔优选】您购买订单号为：" + sendMsgDto.getOrderNo() + " 的商品已退款，款项会在1-3个工作日左右原路返还至您的账户，请耐心等待";
//                content = String.format(SmsConstant.SMS_REFUND_ORDER,sendMsgDto.getOrderNo());
                break;
            case "3":
                content = "【福力兔优选】您购买的订单号为：" + sendMsgDto.getOrderNo() + " 的商品已发货，快递公司为：" + sendMsgDto.getExpressName() + " ，快递单号为：" + sendMsgDto.getExpressNo() + " ，您可以在订单详情中跟踪您的物流轨迹，收货后请检查商品完整并确认收货，有任何疑问请联系客服";
//                content = String.format(SmsConstant.SMS_CONSIGNMENT_ORDER,sendMsgDto.getOrderNo(),sendMsgDto.getExpressName(),sendMsgDto.getExpressNo());
                break;
            case "4":
                content = "【福力兔优选】您已成功购买商品，订单号为：" + sendMsgDto.getOrderNo() + "，我们会尽快为您发货，请耐心等待，有任何疑问请联系客服";
//                content = String.format(SmsConstant.SMS_ENTITY_ORDER,sendMsgDto.getOrderNo());
                break;
            default:
                break;
        }
        if (!SmsUtil.sendCLSmsNotice(content, sendMsgDto.getPhone())){
            // throw new CommonException("短信发送失败，请联系管理员");
            log.error("短信发送失败，请联系管理员");
        }
    }

}
